<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Report_spbu_voucher extends Authenticated_Controller{
	function __construct() {
		parent::__construct();
		$this->load->model('spbu_model','spbu');
		$this->load->model('reservasi_model','reser');
		$this->load->model('pencabang_model','cabang');
	}
	
	function index(){
		$this->list_result();
	}
	
	function list_result($tgl_awal='',$tgl_akhir=''){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('m/d/Y');
		}else{
			$data['tgl_awal']=$this->date_func->change_format_date($tgl_awal,'d-m-Y','m/d/Y');
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('m/d/Y');
		}else{
			$data['tgl_akhir']=$this->date_func->change_format_date($tgl_akhir,'d-m-Y','m/d/Y');
		}

		$data['list_result'] = $this->spbu->get_spbu_voucher_join_setoran($tgl_awal,$tgl_akhir);

		$this->load->view('template/header_view');
		$this->load->view('laporan/laporan_spbu_voucher',$data);
		$this->load->view('template/footer_view');
	}
	
	function cetak_pdf($tgl_awal,$tgl_akhir,$cabang){
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('m/d/Y');
		}else{
			$data['tgl_awal']=$this->date_func->change_format_date($tgl_awal,'d-m-Y','m/d/Y');
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('m/d/Y');
		}else{
			$data['tgl_akhir']=$this->date_func->change_format_date($tgl_akhir,'d-m-Y','m/d/Y');
		}

		$data['cabang']=$cabang;
		
		$data['list_result'] = $this->spbu->get_spbu_voucher_join_setoran_cabang($tgl_awal,$tgl_akhir,$cabang);

		$html = $this->load->view('laporan/html_cetak_laporan_spbu_voucher',$data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Report SPBU Voucher.pdf');
	}
	
	function cetak_excel($tgl_awal,$tgl_akhir,$cabang){
		$this->load->library('excel');
		$data['filter_cabang']=$this->cabang->get_all_cabang();
		if($tgl_awal==''){
			$data['tgl_awal']=date('m/d/Y');
		}else{
			$data['tgl_awal']=$this->date_func->change_format_date($tgl_awal,'d-m-Y','m/d/Y');
		}
		if($tgl_akhir==''){
			$data['tgl_akhir']=date('m/d/Y');
		}else{
			$data['tgl_akhir']=$this->date_func->change_format_date($tgl_akhir,'d-m-Y','m/d/Y');
		}

		$data['cabang']=$cabang;
		
		$data['list_result'] = $this->spbu->get_spbu_voucher_join_setoran_cabang($tgl_awal,$tgl_akhir,$cabang);

		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');

		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan SPBU Voucher');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('B3', $cabang);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Tgl Voucher');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'Kode');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'No. SPJ');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'No. Polisi');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'Supir');
		$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Jurusan');
		$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Jenis BBM');
		$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Jumlah Liter');
		$objPHPExcel->getActiveSheet()->setCellValue('J4', 'Jumlah Rupiah');
		$objPHPExcel->getActiveSheet()->setCellValue('K4', 'SPBU');
		$objPHPExcel->getActiveSheet()->setCellValue('L4', 'Kasir');
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		
		$row=5;
		
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['list_result'][$i]['tanggal']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['list_result'][$i]['kode_spbu']);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $data['list_result'][$i]['NoSPJ']);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['list_result'][$i]['NoPol']);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['list_result'][$i]['driver']);
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $data['list_result'][$i]['asal'].'-'.$data['list_result'][$i]['tujuan']);
			$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $data['list_result'][$i]['jenis_bbm']);
			$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $data['list_result'][$i]['jumlah_liter']);
			$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $data['list_result'][$i]['jenis_uang']);
			$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $data['list_result'][$i]['namaspbu']);
			$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $data['list_result'][$i]['nama']);
		
			$row++;
		}
		$objWriter->save('public/Report SPBU Voucher '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
		redirect(PUBLIC_FOLDER.'Report SPBU Voucher '.$tgl_awal.'-'.$tgl_akhir.'-'.$cabang.'.xls');
	}
}